package com.portal.cwegportal.service;

import com.appcenter.jz.page.PagedResult;
import com.appcenter.jz.unit.ObjectUnit;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.portal.cwegportal.input.usermailaccount.*;
import com.portal.cwegportal.mapper.UserMailAccountMapper;
import com.portal.cwegportal.model.UserMailAccount;
import com.portal.cwegportal.output.usermailaccount.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
/**
 *  创建人:王增立
 *  备注: 用户邮箱的服务已经作废
 *  时间: 2019/4/26 15:55
 *  参数:
 */
@Service
public class UserMailAccountService {

    @Autowired
    UserMailAccountMapper userMailAccountMapper;


    @Transactional
    public EditUserMailAccountOutput ManageUserMailAccount (EditUserMailAccountInput input) throws Exception{
        EditUserMailAccountOutput output=new EditUserMailAccountOutput();
        ManageUserMailAccount(input.getItem().getMail(),input.getItem().getMailPassword());
        output.setItem(new UserMailAccount());
        return  output;
    }

    //根据用户名查询邮箱账号信息
    @Transactional
    public int AddUserMailAccount(String userCode,String mail,String mailPassword){
        UserMailAccount userMailAccount=new UserMailAccount();
        userMailAccount.setGmtCreate(new Date());
        userMailAccount.setGmtModified(new Date());
        userMailAccount.setDeleted(false);
        userMailAccount.setUserName("候新宇");
        userMailAccount.setUserId("houxinyu");
        userMailAccount.setUserCode("houxinyu");
        userMailAccount.setMail(mail);
        userMailAccount.setMailPassword(mailPassword);
        return userMailAccountMapper.insertSelective(userMailAccount);
       // return  ls.size()>0?ls.get(0):null;
    }

    @Transactional
    public void ManageUserMailAccount(String mail,String mailPassword){
        //if()
        UserMailAccount userMailAccount=GetUserMailAccountByUserCode("houxinyu");
        if(userMailAccount!=null)
        {
            UpdateUserMailAccountByUserCode("houxinyu",mail,mailPassword);
        }
        else
        {
            AddUserMailAccount("houxinyu",mail,mailPassword);
        }
    }

    //根据用户名查询邮箱账号信息
    @Transactional
    public UserMailAccount GetUserMailAccountByUserCode(String userCode){
        Example example = new Example(UserMailAccount.class);
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("userCode",userCode);
        List<UserMailAccount> ls=userMailAccountMapper.selectByExample(example);
        return  ls.size()>0?ls.get(0):null;
    }
    //根据用户名修改邮箱账号信息
    @Transactional
    public int UpdateUserMailAccountByUserCode(String userCode,String mail,String mailPassword){
        Example example = new Example(UserMailAccount.class);
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("userCode",userCode);
        UserMailAccount model=new UserMailAccount();
        model.setMail(mail);
        model.setMailPassword(mailPassword);
        model.setGmtModified(new Date());
        return userMailAccountMapper.updateByExampleSelective(model,example);
    }

    @Transactional
    public CreateUserMailAccountOutput createUserMailAccount(CreateUserMailAccountInput input) throws Exception {
        CreateUserMailAccountOutput output = new CreateUserMailAccountOutput();
        UserMailAccount userMailAccount=new UserMailAccount();
        ObjectUnit.Copy(input.getItem(),userMailAccount);
        userMailAccount.setGmtCreate(new Date());
        userMailAccount.setDeleted(false);
        userMailAccountMapper.insertSelective(userMailAccount);
        output.setItem(userMailAccount);
        return output;
    }

    @Transactional
    public EditUserMailAccountOutput editUserMailAccount (EditUserMailAccountInput input) throws Exception{
        EditUserMailAccountOutput output=new EditUserMailAccountOutput();
        UserMailAccount oldEntity= userMailAccountMapper.selectByPrimaryKey(input.getItem().getId());
        oldEntity.setSortNo(input.getItem().getSortNo());
        userMailAccountMapper.updateByPrimaryKey(oldEntity);
        output.setItem(oldEntity);
        return  output;
    }


    public DeleteUserMailAccountOutput deleteUserMailAccount(DeleteUserMailAccountInput input) {
        DeleteUserMailAccountOutput output = new DeleteUserMailAccountOutput();
        UserMailAccount oldEntity = userMailAccountMapper.selectByPrimaryKey(input.getItem().getId());
        oldEntity.setDeleted(true);
        userMailAccountMapper.updateByPrimaryKey(oldEntity);
        return output;
    }

    public GetUserMailAccountsOutput getUserMailAccounts(GetUserMailAccountsInput input) {
        GetUserMailAccountsOutput output = new GetUserMailAccountsOutput();

        //logger.error("log4j2 error!");

        PageHelper.startPage(input.getPage().getPageNo(),input.getPage().getPageSize(),"Id desc");
        //List<Menus> ls = MenuMapper.SearchMenus(input);
        Example example = new Example(UserMailAccount.class);
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("deleted",0);
        List<UserMailAccount> ls=userMailAccountMapper.selectByExample(example);
        PageInfo<UserMailAccount> page = new PageInfo<>(ls);
        output.setItems(new PagedResult<UserMailAccount>(page.getList(),(int)page.getTotal()));
        return  output;

    }


    public PageLoadOutput pageLoad(PageLoadInput input) {
        PageLoadOutput output = new PageLoadOutput();
        output.setItems(getUserMailAccounts(input).getItems());
        return output;
    }
}
